package com.woniu.smallmedical.controller;

import com.woniu.smallmedical.entity.Basicmessage;
import com.woniu.smallmedical.entity.PageBean;
import com.woniu.smallmedical.entity.ResponseResult;
import com.woniu.smallmedical.mapper.BasicmessageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;




@CrossOrigin
@RestController
public class MedicineMessageController {

    @Autowired
    BasicmessageMapper basicmessageapper;



    @GetMapping("/medicine")
    public ResponseResult medicine(Integer pageIndex, Integer pageSize){
        List<Basicmessage> basicmessages = basicmessageapper.selectAll(pageIndex,pageSize);
        PageBean<Basicmessage> pageBean=new PageBean<>();
        pageBean.setPageBeanList(basicmessages);
        pageBean.setPageIndex(pageIndex);
        pageBean.setPageSize(pageSize);
        pageBean.setTotalRecord(basicmessageapper.selectcount());
        return new ResponseResult<PageBean<Basicmessage>>(pageBean,200,"OK");
    }
    //添加学生
    @GetMapping("/medicineinsert")
    public String insertmedicine(String nickname, String medicineformulations, String perscription, String unit, String basicunit, Double purchase, Double retail, String factory, String medicineclassify, String illustrate, String remark, String medicinespecs, Integer Dosage, String frequency, String usages){
        System.out.println("药品名字"+nickname);
        int i = (int)(Math.random()*100+1);
        basicmessageapper.insertmessage(1000+i,nickname,perscription,factory,medicineclassify, medicinespecs,medicineformulations,unit,basicunit,purchase,retail,usages,Dosage,frequency,illustrate,remark,1);
        return "";
    }

    //通过id查
    @GetMapping("/medicineinsertByid")
    public Basicmessage selectbuid( Integer bmid){
        System.out.println("Id是"+bmid);
        Basicmessage basicmessageapper = selectbuid(bmid);

        return basicmessageapper;
    }
    //通过id修改
    @GetMapping("/update")
    public void updatebuid( Integer bmid){
        System.out.println("需要修改状态的ID是"+bmid);
        basicmessageapper.update(bmid);


    }

    //查询信息通过编号
    @GetMapping("/Bynicknamae")
    public ResponseResult selectbuid( String nicknames,Integer pageIndex,Integer pageSize ){

        System.out.println("名字是"+nicknames);
        if(nicknames.equals("") || nicknames==null){
            List<Basicmessage> basicmessages = basicmessageapper.selectAll(pageIndex,pageSize);
            PageBean<Basicmessage> pageBean=new PageBean<>();
            pageBean.setPageBeanList(basicmessages);
            pageBean.setPageIndex(pageIndex);
            pageBean.setPageSize(pageSize);
            pageBean.setTotalRecord(basicmessageapper.selectcount());
            return new ResponseResult<PageBean<Basicmessage>>(pageBean,200,"OK");
        }else {
            List<Basicmessage> basicmessages =basicmessageapper.SelctNumber(nicknames);

            PageBean<Basicmessage> pageBean=new PageBean<>();
            pageBean.setPageBeanList(basicmessages);
            pageBean.setPageIndex(pageIndex);
            pageBean.setPageSize(pageSize);
            pageBean.setTotalRecord(basicmessageapper.selectcount());
            return new ResponseResult<PageBean<Basicmessage>>(pageBean,200,"OK");
        }


    }


}
